/*======================游戏引擎=======================*/
var GameEngine = {
	totalscore:0,
	enemies:{},
	init:function(){
		this.logoShow();
		this.loading();
	},
	logoShow:function(){
		this.logo = document.createElement("div");
		this.logo.className = "logo";
		document.body.appendChild(this.logo);
	},
	loading:function(){
		var load = document.createElement("div");
		load.className = "loading";
		document.body.appendChild(load);
		new Promise(function(resolved){
			var index = 1;
			var t = setInterval(function(){
				/***********************开始动画加载完成**************************/
				if(index > 8){
					clearInterval(t);
					this.logo.remove();
					load.remove();
					resolved();
					return;
				}
				load.style.background = "url(img/loading"+ (index%3+1) +".png)";
				index++;
			}.bind(this),200);
		}.bind(this)).then(this.start);
	},
	bg:{
		ele:document.getElementById("main_body"),
		move:function(){
			setInterval(function(){
				this.ele.style.backgroundPositionY  = parseInt(getComputedStyle(this.ele,null).backgroundPositionY)+ 2 + "px";
			}.bind(this),20);
		},
		left:function(){
			return this.ele.offsetLeft;
		},
		width:function(){
			return this.ele.offsetWidth;
		},
		height:function(){
			return this.ele.offsetHeight;
		}
	},
	enemyshow:function(){
		setInterval(function(){
			this.createSmall();
			this.createMiddle();
			this.createLarge();
		}.bind(this),1000);
	},
	createSmall:function(){
		var num = Math.random();
		if(num>0.4){
			new Enemy(this.ENEMY_TYPE_SMALL).init().move();
		}
	},
	createMiddle:function(){
		var num = Math.random();
		if(num>0.8){
			new Enemy(this.ENEMY_TYPE_MIDDLE).init().move();
		}
	},
	createLarge:function(){
		var num = Math.random();
		if(num>0.94){
			new Enemy(this.ENEMY_TYPE_LARGE).init().move();
		}
	},
	//*****************游戏开始*****************
	start:function(){
		GameEngine.bg.move();
		MyPlane.show();
		GameEngine.enemyshow();
		GameEngine.scoreshow();
	},
	scoreshow:function(){
		var score = document.getElementsByClassName("score")[0];
		score.style.display = "block";
	},
	gameover:function(){
		console.log("游戏结束！");
		var enterBtn = document.getElementsByClassName("enterBtn")[0];
		var shelter = document.getElementsByClassName("shelter")[0];
		shelter.style.display = "block";
		enterBtn.onclick = function(){
			this.restart();
		}.bind(this)
	},
	restart:function(){
		location.reload();
	},
	ENEMY_TYPE_SMALL:1,
	ENEMY_TYPE_MIDDLE:2,
	ENEMY_TYPE_LARGE:3
	
}